package com.yanqu.road.server.cmd.activity.suitang.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.suitang.config.SuiTangConfigSync;
import com.yanqu.road.entity.activity.tonbing.config.TonBingConfig;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.TonBingProto;
import com.yanqu.road.pb.activity.suitang.SuiTang;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityInfoMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.suitang.SuiTangMgr;
import com.yanqu.road.server.manger.activity.tonbing.TonBingMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.text.MessageFormat;

@Cmd(code = GameProtocol.S_SUI_TANG_AUTO_SEND_CITY_COLLECT, desc = "")
public class SuiTangMailSendCollectFromCrossCmd implements Command {

    private Logger logger = LogManager.getLogger(SuiTangMgr.class.getName());

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {

        logger.debug("sui tang auto send collect reward begin...");

        SuiTangConfigSync config = SuiTangMgr.getConfig();
        if (config == null) {
            logger.error("sui tang auto send collect reward fail, no activity!");
            return;
        }

        SuiTang.CrossSuiTangMailSendCityCollectReqMsg reqMsg = SuiTang.CrossSuiTangMailSendCityCollectReqMsg.parseFrom(packet.getBytes());
        for (SuiTang.CrossSuiTangCityCollectMsg msg : reqMsg.getListList()) {
            long userId = msg.getUserId();
            long count = msg.getCount();
            long muBingLing = msg.getMuBingLing();
            String reward = "";
            if (count > 0) {
                reward = config.getParamConfig().getJunZiItem() + "=" + count;
            }
            if (muBingLing > 0) {
                if (reward.isEmpty()) {
                    reward = config.getParamConfig().getMuBingLingItem() + "=" + muBingLing;
                } else {
                    reward += ";" + config.getParamConfig().getMuBingLingItem() + "=" + muBingLing;
                }
            }
            if (reward.isEmpty()) {
                return;
            }

            logger.debug("sui tang auto send collect reward : userId {}, count {}, mubingling {}.", userId, count, muBingLing);

            UserInfo userInfo = UserMgr.getUserInfo(userId);
            if (userInfo == null) {
                logger.error("sui tang auto send collect reward fail, no userInfo : userId {}, count {}, mubingling {}.", userId, count, muBingLing);
                continue;
            }

            String language = userInfo.getLanguage();
            String activityName = ActivityMgr.getActivityName(config.getActivityInfo(), language);
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.SUITANG_MAIL_TITLE_FOOD_REWORD, language), activityName);
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.SUITANG_MAIL_CONTENT_FOOD_REWORD, language), activityName);

            MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), reward, mailContent, mailTitle, config.getActivityInfo().getActivityId(), config.getActivityInfo().getServerListStr());
        }
    }

}
